package controller;

import validate.ZanValidate;
import model.User;
import model.Zan;

import com.jfinal.core.Controller;

import constant.Msg;

public class ZanController extends Controller {
	public void index(){
		int userId=User.userCache.get(getHeader("token"));
		String typeId=getPara("type_id");
		String type=getPara("type");
		String validate=null;
		String offset=getPara("offset");
		String length=getPara("length");
		switch (getRequest().getMethod()) {
		case "GET": 
			validate=ZanValidate.get(type, offset, length);
			if (validate!=null) {
				renderJson(validate);
				return;
			}
			if(offset==null) offset = "0";
			if(length==null) length = "10";
			renderJson(Zan.dao.getZans(userId, type, offset, length));
			break;
		case "POST":
			validate=ZanValidate.post(type, typeId);
			if (validate!=null) {
				renderJson(validate);
				return;
			}
			renderJson(Zan.dao.like(userId, type, typeId));
			break;
		case "DELETE":
			validate=ZanValidate.delete(type, typeId);
			if (validate!=null) {
				renderJson(validate);
				return;
			}
			renderJson(Zan.dao.cancel(userId, type, typeId));
			break;
		default:renderJson(Msg.SUCCESS_EXECUTE);
			break;
		}
	}
	
	public void check(){
		int userId=User.userCache.get(getHeader("token"));
		String typeId=getPara("type_id");
		String type=getPara("type");
		String validate = null;
		switch (getRequest().getMethod()) {
		case "GET": 
			validate=ZanValidate.check(type, typeId);
			if (validate!=null) {
				renderJson(validate);
				return;
			}
			renderJson(Zan.dao.checkstar(userId, type, typeId));
			break;
		default:renderJson(Msg.SUCCESS_EXECUTE);
			break;
		}
	}
}
